﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Model.Department;
using Enterprise.Service.Department;
using Enterprise.Infrastructure;
using Enterprise.Control;
namespace Enterprise.UI.Web.Manage.DepartMent
{
    public partial class List : System.Web.UI.Page
    {
        protected int Id = (int)Utility.sink("Id", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        int dId = (int)Utility.sink("dId", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        string Action = (string)Utility.sink("Action", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Action) && dId != 0)
                {
                    DepartmentService dService = new DepartmentService();
                    switch (Action)
                    {
                        case "Up":
                            dService.DepartmentMoveUp(dId);
                            break;
                        case "Down":
                            dService.DepartmentMoveDown(dId);
                            break;
                    }
                }
                OnStart();
            }
        }

        private void OnStart()
        {
            string url = "Manage.aspx";
            CreateBT.AddButton(url, Utility.PopedomType.New, HeadMenu1);
            CreateBT.AddButton("orderby.gif", "排序", "List.aspx?Action=OrderBy&Id=" + Id, Utility.PopedomType.Orderby, HeadMenu1);
            OnBindData();
        }

        private void OnBindData()
        {
            DepartmentService ser = new DepartmentService();
            var q = ser.DepartmentList().Where(p => p.DPARENTID == Id).OrderBy(p => p.DORDERBY).ToList();
            GridView1.DataSource = q;
            GridView1.DataBind();
        }

        protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (string.IsNullOrEmpty(Action))
            {
                e.Row.Cells[3].Visible = false;
            }
        }

        //#region 绑定TreeView
        ///// <summary>
        ///// 绑定TreeView（利用TreeNode）
        ///// </summary>
        ///// <param name="p_Node">TreeNode（TreeView的一个节点）</param>
        ///// <param name="pid_val">父id的值</param>
        ///// <param name="id">数据库 id 字段名</param>
        ///// <param name="pid">数据库 父id 字段名</param>
        ///// <param name="text">数据库 文本 字段值</param>
        //protected void Bind_Tv(List<DepartmentEntity> dt, TreeNode pNode, int pId)
        //{
        //    TreeNode tn;//建立TreeView的节点（TreeNode），以便将取出的数据添加到节点中            
        //    List<DepartmentEntity> relist = dt.Where(p => p.DPARENTID == pId).OrderBy(p => p.DORDERBY).ToList();//利用DataView将数据进行筛选，选出相同 父id值 的数据
        //    foreach (DepartmentEntity row in relist)
        //    {
        //        tn = new TreeNode();//建立一个新节点（学名叫：一个实例）
        //        if (pNode == null)//如果为根节点
        //        {
        //            tn.Value = row.DEPTID.ToString();//节点的Value值，一般为数据库的id值
        //            tn.Text = string.Format("<a href=\"List.aspx?Id={0}\">{1}</a>", row.DEPTID, row.DNAME);//节点的Text，节点的文本显示
        //            TreeView1.Nodes.Add(tn);//将该节点加入到TreeView中
        //            Bind_Tv(dt, tn, row.DEPTID);//递归（反复调用这个方法，直到把数据取完为止）
        //        }
        //        else//如果不是根节点
        //        {
        //            tn.Value = row.DEPTID.ToString();//节点Value值
        //            tn.Text = string.Format("<a href=\"List.aspx?Id={0}\">{1}</a>", row.DEPTID, row.DNAME);//节点Text值
        //            pNode.ChildNodes.Add(tn);//该节点加入到上级节点中
        //            Bind_Tv(dt, tn, row.DEPTID);//递归
        //        }
        //    }
        //}
        //#endregion
    }
}